-- Profiler
-- 性能测试器
-- 没什么用-_-||
Profiler = class()
Profiler.MaxMemoryUse = 384 * 1024
Profiler.HighMemoryUse = 128 * 1024
Profiler.SafetyMemoryUse = 64 * 1024
Profiler.Stop = 0
Profiler.Collecting = 1

function Profiler:init()
    self.state = Profiler.Stop
    collectgarbage("stop")
    self.lastTime = ElapsedTime
    self:update()
end

function Profiler:getMem()
    return math.ceil(self.mem // 10 * 10 / 1024.0)
end

function Profiler:update()
    self.mem = collectgarbage("count")
    if ElapsedTime - self.lastTime > 5 then
        if self.state == Profiler.Stop and self.mem > Profiler.HighMemoryUse then
            collectgarbage("restart")
            self.state = Profiler.Collecting
        elseif self.state == Profiler.Collecting and self.mem < Profiler.SafetyMemoryUse then
            collectgarbage("stop")
            self.state = Profiler.Stop
        elseif self.mem > Profiler.MaxMemoryUse then
            collectgarbage("collect")
        end
        self.lastTime = ElapsedTime
    end
end
